<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .ad {
            width: 140px;
            height: 300px;
            left: 0;
            position: absolute;
            background-color: purple;
        }
    </style>
</head>
<body>
    <div class="ad">
        <div class="clo" onclick="clo()">X</div>
        <strong>三秒自动关闭广告</strong>
    </div>
    <button onclick="cancelTimer()">点击取消定时器</button>
    <script>
        function clo(event) {
            if (!event) {
                var ad = document.querySelector('.ad');
                ad.style.display = 'none';
                return;
            }
            event.target.parentNode.style.display = 'none';
        }
        function func() {
            clo(null);
        }
        setTimeout(func, 3000);
        function cancelTimer() {
            window.clearTimeout(clo);
        }
    </script>
</body>
</html>